package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCartBean;
import com.project.bean.SaleCartBean;
import com.project.mapper.ISaleCartMapper;
import com.project.service.ISaleCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SaleCartServiceImp implements ISaleCartService {
    @Autowired
    private ISaleCartMapper mapper;
    @Override
    public List<SaleCartBean> findById(Integer userId) {
        return mapper.findById(userId);
    }

    @Override
    public void update(Integer id, Integer num) {
        SaleCartBean saleCartBean = mapper.selectById(id);
        saleCartBean.setSaleNum(num);
        mapper.updateById(saleCartBean);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void delByUserId(Integer userId) {
        mapper.deleteById(userId);
    }

    @Override
    public void add(String shopId,Integer userId ) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("fk_shopId",shopId);
        qw.eq("fk_userId",userId);
        SaleCartBean saleCartBean =mapper.selectOne(qw);
        if(saleCartBean==null){
            SaleCartBean saleCartBean1 = new SaleCartBean();
            saleCartBean1.setSaleNum(1);
            saleCartBean1.setStockShopId(shopId);
            saleCartBean1.setUserId(userId);
            mapper.insert(saleCartBean1);
        }else {
            saleCartBean.setSaleNum(saleCartBean.getSaleNum()+1);
            mapper.updateById(saleCartBean);
        }
    }
}
